package be.redo.model.entities;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.format.annotation.NumberFormat;

/**
 * @author Frank Plain old Java class to represent a database table row.
 *         Represents a discount in the domain model.
 * 
 */
@Entity
@NamedQueries({
		@NamedQuery(name = "findAantalDiscounts", query = "select count(d) from Discount d"),
		@NamedQuery(name = "findAllDiscounts", query = "select d from Discount d order by d.vacation"),
		@NamedQuery(name = "findDiscountsByVacation", query = "select d from Discount d where d.vacation = :vacation") })
public class Discount implements Serializable {

	/**
	 * Generated serialVersionUID.
	 */
	private static final long serialVersionUID = 2317005259529004811L;

	/**
	 * Amount of the discount in euros. Is a positive number>=0.
	 * 
	 */
	@NotNull
	@Min(0)
	@NumberFormat(pattern = "#,##0.00")
	private BigDecimal amount;

	/**
	 * Reason why the customer gets a discount.
	 */
	@NotNull
	@Min(3)
	private String description;

	/**
	 * Vacation for which the customer gets a discount.
	 */
	@NotNull
	@ManyToOne
	@JoinColumn(name = "vacation")
	private Vacation vacation;

	public BigDecimal getAmount() {
		return this.amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Vacation getVacation() {
		return this.vacation;
	}

	public void setVacation(Vacation vacation) {
		this.vacation = vacation;
	}

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

	/**
	 * Generated key by the Database
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "discount_id")
	private Long DiscountId;

	public Long getDiscountId() {
		return this.DiscountId;
	}

	public void setDiscountId(Long id) {
		this.DiscountId = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((amount == null) ? 0 : amount.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result
				+ ((vacation == null) ? 0 : vacation.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Discount other = (Discount) obj;
		if (amount == null) {
			if (other.amount != null) {
				return false;
			}
		} else if (!amount.equals(other.amount)) {
			return false;
		}
		if (description == null) {
			if (other.description != null) {
				return false;
			}
		} else if (!description.equals(other.description)) {
			return false;
		}
		if (vacation == null) {
			if (other.vacation != null) {
				return false;
			}
		} else if (!vacation.equals(other.vacation)) {
			return false;
		}
		return true;
	}

}
